﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR;
using System.Threading.Tasks;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.SignalR
{
    public class ConnectionMapping : PersistentConnection
    {
        protected override Task OnConnectedAsync(IRequest request, string connectionId)
        {
            string sesseionId = request.Cookies["ASP.NET_SessionId"] != null ? request.Cookies["ASP.NET_SessionId"].Value : "";
            if (sesseionId != "")
            {
                if (!Common.GlobalConstant.CLIENT_LIST.Keys.Contains(sesseionId))
                {
                    Common.GlobalConstant.CLIENT_LIST.Add(sesseionId, new List<string> { connectionId });
                }
                else
                {
                    if (!Common.GlobalConstant.CLIENT_LIST[sesseionId].Contains(connectionId))
                    {
                        Common.GlobalConstant.CLIENT_LIST[sesseionId].Add(connectionId);
                    }
                }
            }
            return null;

        }

        //protected override Task OnReconnectedAsync(IRequest request, IEnumerable<string> groups, string clientId)
        //{
        //    return Connection.Broadcast("Client " + clientId + " re-connected");
        //}

        //protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
        //{
        //    var info = data + ". ConnectionId is [" + connectionId + "]";
        //    // return Connection.Send(connectionId, info);   

        //    // Broadcast data to all clients
        //    return Connection.Broadcast(info);
        //}
        
        protected override Task OnDisconnectAsync(string connectionId)
        {
            
            foreach(KeyValuePair<string,List<string>> clientList in Common.GlobalConstant.CLIENT_LIST)
            {
                bool isMatch = false;
                foreach (string s in clientList.Value)
                {
                    if (s == connectionId)
                    {
                        clientList.Value.Remove(s);
                        if (clientList.Value.Count == 0)
                        {
                            Common.GlobalConstant.CLIENT_LIST.Remove(clientList.Key);
                        }
                        Common.GlobalConstant.SESSION_USER_LIST.Remove(clientList.Key);
                        isMatch = true;
                        break;
                    }
                }
                if (isMatch)
                {
                    break;
                }
            }

            return Connection.Broadcast("Connection " + connectionId + " disconncted");

            
        }

        //protected override Task OnErrorAsync(Exception error)
        //{
        //    return Connection.Broadcast("Error ocurred " + error);
        //}
    }
}